const app = getApp()
var that;
var util = require('../../utils/util.js');
Page({

  data: {

  },


  onLoad: function (options) {
    that = this;
    
    // 判断登陆状态
    if (app.globalData.checkLogin) {
      console.log('判断onLaunch是否执行完毕1')
      if (app.globalData.loginUserObj != null) {
        // 获取会议室列表
        that.getRoomListFun();
      }
    } else {
      app.checkLoginReadyCallback = res => {
        console.log('判断onLaunch是否执行完毕2', res)
        if (app.globalData.loginUserObj != null) {
          // 获取会议室列表
          that.getRoomListFun();
        }
      };
    }
  },

  // 快速创建会议
  toCreatMeetingFun: function () {
    wx.setStorage({
      key: 'roomList',
      data: that.data.roomList,
      success: function () {
        wx.navigateTo({
          url: '/pages/meetingCreate/meetingCreate',
        })
      }
    })
  },


  // 获取会议室列表
  getRoomListFun: function () {
    wx.request({
      url: app.http + 'hysMeetingRoom/meetingRoomList',
      data: {
        companyId: app.globalData.loginUserObj.ydCompany.id,
      },
      method: 'get',
      header: {
        'content-type': 'application/json', // 默认值
        'APP-User-Token': app.globalData.tokenMap.access_token
      },
      success: function (res) {
        console.log("获取会议室列表suc返回", res)
        if (res.data.code == 200) {
          that.setData({
            roomList: res.data.data
          })
          console.log("data", that.data)
        }
        else if (res.data.code == 100101) {
          // token无效，获取新的token
          var newToken = util.refreshTokenFun(app.globalData.tokenMap.refresh_token).then(
            function (res) {
              if (res.data.code == 200) {
                that.getRoomListFun();
              }
              else if (res.data.code == 400) {
                var wxLoginFun = util.wxLoginFun().then(
                  function (res2) {
                    if (res2.data.code == 200) {
                      that.getRoomListFun();
                    }
                  }
                )
              }
            }
          ).catch(
            function (err) {
              console.log("获取新的token失败返回", err)
            }
          )
        }
      },
      fail: function (err) {
        console.log("fail返回", err)
      }
    })
  },

  // 跳转会议室详情
  toRoomDetailFun: function (e) {
    let index = e.currentTarget.dataset.index;
    wx.setStorage({
      key: 'roomItemObj',
      data: that.data.roomList[index],
      success: function () {
        wx.navigateTo({
          url: '/pages/roomDetail/roomDetail',
        })
      }
    })
  },



})